package com.thinkcube.edutainment;

import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.ImageView;
import android.widget.RatingBar;
import android.widget.TextView;

import com.bugsense.trace.BugSenseHandler;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.thinkcube.edutainment.domain.data.CourseData;
import com.thinkcube.edutainment.domain.data.PaymentData;
import com.thinkcube.edutainment.fragment.PaymentPlanFragment;
import com.thinkcube.edutainment.fragment.PaymentPlanFragment_;

@EActivity
public class PaymentActivity extends Activity {

	Intent intent;
	CourseData courseData;
	DisplayImageOptions options;
	ImageLoader imageLoader;

	@ViewById(R.id.course_icon)
	ImageView courseIcon;
	
	@ViewById(R.id.ratingbar_course_detail)
	RatingBar ratingBar;
	
	@ViewById(R.id.student_cunt_de)
	TextView studentCount;
	
	@ViewById(R.id.course_title)
	TextView courseTitle;
	
	@ViewById(R.id.code)
	TextView courseCode;
	
	@ViewById(R.id.course_price_de)
	TextView priceDescription;
	
	@ViewById(R.id.price_description)
	TextView paymentDescription;

	PaymentData paymentData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		options = new DisplayImageOptions.Builder()
				.showStubImage(R.drawable.icon)
				.showImageForEmptyUri(R.drawable.icon)
				.showImageOnFail(R.drawable.icon).cacheOnDisc(true)
				.displayer(new RoundedBitmapDisplayer(5)).build();

		imageLoader = ImageLoader.getInstance();
		
		BugSenseHandler.initAndStartSession(PaymentActivity.this,
				EdutainmentConstant.BUG_SENSE_APIKEY);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.payment_main);
		getWindow().setBackgroundDrawableResource(R.drawable.payment_border);
		
		

		if (getIntent().hasExtra("paymentData")) {
			
			paymentData = (PaymentData) getIntent().getExtras()
					.get("paymentData");
			
		}

		if (getIntent().hasExtra("courseObject")) {
			
			courseData = (CourseData) getIntent().getExtras().get(
					"courseObject");
			
			setCourseData();
		}

		addPaymentPlanUI();
	}

	/* add dynamic fragments to activity */
	void addPaymentPlanUI() {
		FragmentTransaction fragmentTransaction = getFragmentManager()
				.beginTransaction();
		Bundle bundle = new Bundle();
		
			if(courseData!=null)
				{
					bundle.putParcelable("paymentData", paymentData);
					bundle.putParcelable("courseObject", courseData);
				}
			
		PaymentPlanFragment paymentplan = new PaymentPlanFragment_();
		paymentplan.setArguments(bundle);
		fragmentTransaction.add(R.id.payment_details, paymentplan);
		fragmentTransaction.commit();
	}

	/* set course data on the layout using parsalable data */
	void setCourseData() {
		
		if (getIntent() != null) {
			
			courseData = getIntent().getParcelableExtra("courseObject");
			
			imageLoader.displayImage(courseData.getCourseIconLarge(),
					courseIcon, options);
			
			studentCount.setText(courseData.getNumOfStudents());
			
			courseTitle.setText(courseData.getCourseTitle());
			
			priceDescription.setText("LKR "+Util.numberFormat(courseData.getCoursePrice()));
			
			if(courseData.getSubscription_plan().equals("2"))
			{
				paymentDescription.setText(getString(R.string.payment_des_sub));
			}
			else{
				paymentDescription.setText(getString(R.string.pay_des_full));
			}
			
			courseCode.setText("(CODE:" + courseData.getCourseCode() + ")");
			try {
				if (courseData.getCourseRating() != null) {
					ratingBar.setRating(Float.valueOf(courseData
							.getCourseRating()));
				}
			} catch (Exception e) {

			}

		}
	}
	
	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();

		BugSenseHandler.closeSession(PaymentActivity.this);
	}


}
